package cn.medemede.servlet.action;

import cn.medemede.dao.UserpwdDao;
import cn.medemede.dao.UsersDao;
import cn.medemede.model.UserpwdBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.io.Serializable;

public class Log_check implements Action,Serializable{

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String farward;
        PrintWriter out = response.getWriter();
        String username = request.getParameter("username");
        System.out.println("username:" + username);
        String password = request.getParameter("password");
        System.out.println("password:" + password);
        String code = (String) request.getSession().getAttribute("code");
        code=code.toLowerCase();
        System.out.println("code:" + code);
        String icode = request.getParameter("checkcode");
        icode=icode.toLowerCase();
        System.out.println("checkcode:" + icode);

        UserpwdBean userpwdBean = new UserpwdBean();
        userpwdBean.setUsername(username);
        userpwdBean.setPassword(password);

        if (code.equals(icode)){
            System.out.println("验证码正确！");
            if (UserpwdDao.find(userpwdBean)){
                farward = "log_suc";
                HttpSession session=request.getSession();
                //session.setAttribute("username",username);  //存储UserName
                session.setAttribute("user", UsersDao.find(username));//存储User对象
            }else{
                farward="log_failed";
                out.println("密码错误或用户名不存在！");
            }
        }else {
            farward="验证码错误！";
        }

        return farward;
    }
}
